/*
   Copyright (c) 2011, 2015 Mizar Tools Contributors (mizartools.org)

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
/*  Contributors :
 *	2011-02-05 Marco Riccardi - initial implementation
 *	2011-03-20 Marco Riccardi - added method changeAllSpecialChars(String value)
 *	2015-01-11 Marco Riccardi - added method changeAllSpecialCharsHex(String value)
 *                            - and changeCharsHex(String value)
 *
 */
package org.mizartools.utility;

public class Html {

	public static String changeChars(String value){
		return value.replaceAll("&", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;").replaceAll("\"", "&quot;");
	}

	public static String changeCharsHex(String value){
		StringBuffer sb = new StringBuffer("");
		char c;
		for (int index = 0 ;  index < value.length() ; index++) {
			c = value.charAt(index);
			int i = (int)c;
			switch (i) {
			case 34 : // "
			case 36 : // $
			case 37 : // %
			case 39 : // '
			case 40 : // (
			case 41 : // )
			case 42 : // *
			case 43 : // +
			case 60 : // <
			case 62 : // >
			case 94 : // ^
			case 96 : // `
			case 123 : // {
			case 124 : // |
			case 125 : // }
			case 126 : // ~
				sb.append("&#x"+Integer.toHexString((int)c).toUpperCase()+";");
				break;
			case 35 : // #
				if ((index+1) < value.length() && index > 0) {
					if ((int)value.charAt(index-1) == 38 && (int)value.charAt(index+1) == 120){
						sb.append(c);
					} else {
						sb.append("&#x"+Integer.toHexString((int)c).toUpperCase()+";");
					}
				} else {
					sb.append("&#x"+Integer.toHexString((int)c).toUpperCase()+";");
				}
				break;
			case 38 : // &
				if ((index+2) < value.length()) {
					if ((int)value.charAt(index+1) == 35 && (int)value.charAt(index+2) == 120){
						sb.append(c);
					} else {
						sb.append("&#x"+Integer.toHexString((int)c).toUpperCase()+";");
					}
				} else {
					sb.append("&#x"+Integer.toHexString((int)c).toUpperCase()+";");
				}
				break;
			default : sb.append(c);}
		}
		return sb.toString();
	}
	
	public static String changeAllSpecialChars(String value){
		value = changeChars(value);
		StringBuffer sb = new StringBuffer("");
		char c;
		for (int index = 0 ;  index < value.length() ; index++) {
			c = value.charAt(index);
			if ((int)c > 126) {
				sb.append("&#"+(int)c+";");
			} else {
				sb.append(c);
			}
		}
		return sb.toString();
	}

	public static String changeAllSpecialCharsHex(String value){
		value = changeCharsHex(value);
		StringBuffer sb = new StringBuffer("");
		char c;
		for (int index = 0 ;  index < value.length() ; index++) {
			c = value.charAt(index);
			if ((int)c > 126) {
				sb.append("&#x"+Integer.toHexString((int)c).toUpperCase()+";");
			} else {
				sb.append(c);
			}
		}
		return sb.toString();
	}
	
	private Html(){}
}
